package org.maylove.wuziqi.service;

import org.maylove.wuziqi.dao.QuestionDao;
import org.maylove.wuziqi.pojo.question.Question;
import org.maylove.wuziqi.pojo.question.dto.GetQuestionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionDao questionDao;

    @Override
    public void addQuestion(Question question) {
        question.setQuestionId(java.time.Instant.now().toEpochMilli());
        questionDao.save(question);
    }

    @Override
    public Long getUserId(Long questionId) {
        Question question = questionDao.findByQuestionId(questionId);
        if(question == null) {
            return 0L;
        }
        return question.getUserId();
    }

    @Override
    public void deleteQuestion(Long questionId) {
        questionDao.deleteByQuestionId(questionId);
    }

    @Override
    public List<Question> getQuestion(GetQuestionDto getQuestionDto) {
        return questionDao.getQuestion(getQuestionDto.getPageSize()*(getQuestionDto.getPageNum()-1), getQuestionDto.getPageSize());
    }

    @Override
    public void addAnswer(Long questionId, String answer) {
        Question question = questionDao.findByQuestionId(questionId);
        if(question != null) {
            question.setAnswer(answer);
            questionDao.save(question);
        }
    }

    @Override
    public void deleteAnswer(Long questionId) {
        Question question = questionDao.findByQuestionId(questionId);
        question.setAnswer("等待作者回答中...");
        questionDao.save(question);
    }


}
